package com.wdb007.venus.svc.biz.staff;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.wdb007.venus.svc.biz.utils.DateFormatUtil;
import com.wdb007.venus.svc.dal.book.dao.BookGridTransDAO;
import com.wdb007.venus.svc.dal.book.dao.BookShelfDAO;
import com.wdb007.venus.svc.dal.book.model.BookGridTransDO;
import com.wdb007.venus.svc.dal.book.model.BookShelfDO;
import com.wdb007.venus.svc.dal.staff.dao.StaffAreaInfoDAO;
import com.wdb007.venus.svc.dal.staff.dao.StaffDistrictInfoDAO;
import com.wdb007.venus.svc.dal.staff.dao.StaffInfoDAO;
import com.wdb007.venus.svc.dal.staff.dao.StaffShelfRegisterDAO;
import com.wdb007.venus.svc.dal.staff.dao.StaffTransposingDAO;
import com.wdb007.venus.svc.dal.staff.model.StaffAreaInfoDO;
import com.wdb007.venus.svc.dal.staff.model.StaffDistrictInfoDO;
import com.wdb007.venus.svc.dal.staff.model.StaffInfoDO;
import com.wdb007.venus.svc.dal.staff.model.StaffShelfRegisterDO;
import com.wdb007.venus.svc.dal.staff.model.StaffShelfRegisterPageDO;
import com.wdb007.venus.svc.dal.staff.model.StaffTransposingDO;
import com.wdb007.venus.svc.dal.staff.model.StaffTransposingPageDO;
import com.wdb007.venus.svc.facade.book.model.BookgridUnlockDTO;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;
import com.wdb007.venus.svc.facade.staff.StaffInfoService;
import com.wdb007.venus.svc.facade.staff.model.AreaInfo;
import com.wdb007.venus.svc.facade.staff.model.ChangeBookInfo;
import com.wdb007.venus.svc.facade.staff.model.DistrictInfo;
import com.wdb007.venus.svc.facade.staff.model.LockSuccDTO;
import com.wdb007.venus.svc.facade.staff.model.LoginRspDTO;
import com.wdb007.venus.svc.facade.staff.model.ShelfInfoDTO;
import com.wdb007.venus.svc.facade.staff.model.ShelfInfoLi;
import com.wdb007.venus.svc.facade.staff.model.ShelfListPage;
import com.wdb007.venus.svc.facade.staff.model.ShelfRegisterReqDTO;

@Service("staffInfoService")
public class StaffInfoServiceImpl implements StaffInfoService {

	@Autowired
	private StaffInfoDAO staffInfoDAO;
	
	@Autowired
	private StaffShelfRegisterDAO staffShelfRegisterDAO;
	
	@Autowired
	private StaffTransposingDAO staffTransposingDAO;
	
	@Autowired
	private BookShelfDAO bookShelfDAO;
	
	@Autowired
	private StaffDistrictInfoDAO staffDistrictInfoDAO;
	
	@Autowired
	private StaffAreaInfoDAO staffAreaInfoDAO;
	
	@Autowired
	private BookGridTransDAO bookGridTransDAO;
	@Override
	public LoginRspDTO login(String loginName, String loginPwd) throws BizException{
		
		if(ObjectUtils.isEmpty(loginName) || ObjectUtils.isEmpty(loginPwd))
			throw new BizException(RspCodeEnum.STAFF_LOGIN_NAME_OR_PWD_IS_EMPTY);
		StaffInfoDO staffInfo = staffInfoDAO.getByLoginName(loginName);
		if(staffInfo == null)
			throw new BizException(RspCodeEnum.STAFF_LOGIN_NAME_IS_INVALID);
		if(!loginPwd.equals(staffInfo.getLoginPwd()))
			throw new BizException(RspCodeEnum.STAFF_LOGIN_PWD_IS_INVALID);
		
		LoginRspDTO loginRsp = new LoginRspDTO();
		loginRsp.setUserid(staffInfo.getId());
		loginRsp.setUsertoken(staffInfo.getStaffToken());
		loginRsp.setUser_status(staffInfo.getStaffStatus());
		
		int shelfCount = staffShelfRegisterDAO.countByUserId(staffInfo.getId());
		loginRsp.setBookshelf_count(shelfCount);
		int bookchangeCount = staffTransposingDAO.countByUserId(staffInfo.getId());
		loginRsp.setChangebook_count(bookchangeCount);
		return loginRsp;
	}

	@Override
	public void shelfRegister(ShelfRegisterReqDTO registerDTO) throws BizException {
		
		String bookshelfCode = registerDTO.getBookshelf_code();
		BookShelfDO bookshelf = bookShelfDAO.get(bookshelfCode);
		StaffShelfRegisterDO registerResult = staffShelfRegisterDAO.getByShelfCode(bookshelfCode);
		if(null == bookshelf) {
			StaffShelfRegisterDO registerDO = new StaffShelfRegisterDO();
			registerDO.setAreaName(registerDTO.getArea_name());
			registerDO.setBookshelfCode(registerDTO.getBookshelf_code());
			registerDO.setCreateTime(new Date());
			registerDO.setStaffId(registerDTO.getUserid());
			staffShelfRegisterDAO.insert(registerDO);
			
			BookShelfDO bookShelfDO = new BookShelfDO();
			bookShelfDO.setAddress(registerDTO.getAddress());
			bookShelfDO.setArea(registerDTO.getArea_name());
			bookShelfDO.setAvailable(1);
			bookShelfDO.setCode(registerDTO.getBookshelf_code());
			bookShelfDO.setColCount(4);
			bookShelfDO.setRowCount(4);
			bookShelfDO.setCreateTime(new Date());
			bookShelfDO.setDamageReport(0);
			bookShelfDO.setDistrictCode(registerDTO.getDistrict_code());
			bookShelfDO.setExtraInfo(registerDTO.getExtra_info());
			bookShelfDO.setGcj02Latitude(registerDTO.getLatutude());
			bookShelfDO.setGcj02Longitude(registerDTO.getLontitude());
			bookShelfDO.setLatitude(registerDTO.getLatutude());
			bookShelfDO.setLongitude(registerDTO.getLontitude());
			bookShelfDAO.insert(bookShelfDO);
		}else {
			bookshelf.setAddress(registerDTO.getAddress());
			bookshelf.setArea(registerDTO.getArea_name());
			bookshelf.setDistrictCode(registerDTO.getDistrict_code());
			bookshelf.setExtraInfo(registerDTO.getExtra_info());
			bookshelf.setGcj02Latitude(registerDTO.getLatutude());
			bookshelf.setGcj02Longitude(registerDTO.getLontitude());
			bookshelf.setLatitude(registerDTO.getLatutude());
			bookshelf.setLongitude(registerDTO.getLontitude());
			bookShelfDAO.update(bookshelf);
			
			registerResult.setAreaName(registerDTO.getArea_name());
			registerResult.setModifyTime(new Date());
			staffShelfRegisterDAO.update(registerResult);
		}
	}

	@Override
	public List<DistrictInfo> getDistrict(String province) throws BizException {
		List<StaffDistrictInfoDO> list = staffDistrictInfoDAO.listByCityCode(province);
		if(ObjectUtils.isEmpty(list)) {
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		}
		List<DistrictInfo> di = new ArrayList<DistrictInfo>(list.size());
		for(StaffDistrictInfoDO sd : list) {
			DistrictInfo info = new DistrictInfo();
			info.setDistrict_code(sd.getDistrictCode());
			info.setDistrict_name(sd.getDistrictName());
			di.add(info);
		}
		return di;
	}

	@Override
	public List<AreaInfo> getAreaList(String districtCode, String queryName) throws BizException {
		StaffAreaInfoDO query = new StaffAreaInfoDO();
		query.setDistrictCode(districtCode);
		List<StaffAreaInfoDO> list = staffAreaInfoDAO.listByCondition(query);
		if(ObjectUtils.isEmpty(list)) {
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		}
		List<AreaInfo> aiList =new ArrayList<AreaInfo>(list.size());
		for(StaffAreaInfoDO sd : list) {
			AreaInfo ai = new AreaInfo();
			ai.setArea_name(sd.getAreaName());
			aiList.add(ai);
		}
		return aiList;
	}

	@Override
	public List<ShelfInfoLi> getShelfList(ShelfListPage page) throws BizException {
		
		StaffShelfRegisterPageDO pageDO  = new StaffShelfRegisterPageDO();
		pageDO.setStaffId(page.getUserid());
		pageDO.setOffset(page.getOffset());
		pageDO.setPagesize(page.getPagesize());
		List<StaffShelfRegisterDO> list = staffShelfRegisterDAO.listByUser(pageDO);
		if(ObjectUtils.isEmpty(list))
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		List<ShelfInfoLi> lilist = new ArrayList<ShelfInfoLi>(list.size());
		for(StaffShelfRegisterDO register : list) {
			ShelfInfoLi info = new ShelfInfoLi();
			info.setArea_name(register.getAreaName());
			info.setBookshelf_code(register.getBookshelfCode());
			info.setRegister_time(DateFormatUtil.formatDateTime(register.getCreateTime()));
			lilist.add(info);
		}
		return lilist;
	}

	@Override
	public ShelfInfoDTO getShelfInfo(String bookshelfCode) throws BizException {
		
		BookShelfDO bookShelf = bookShelfDAO.get(bookshelfCode);
		if(null == bookShelf)
			throw new BizException(RspCodeEnum.BOOKSHELF_IS_NOT_EXISTS);
		ShelfInfoDTO shelfInfo = new ShelfInfoDTO();
		shelfInfo.setAddress(bookShelf.getAddress());
		shelfInfo.setBookshelf_code(bookShelf.getCode());
		shelfInfo.setDistrict_code(bookShelf.getDistrictCode());
		shelfInfo.setArea_name(bookShelf.getArea());
		shelfInfo.setExtra_info(bookShelf.getExtraInfo());
		shelfInfo.setLatutude(bookShelf.getGcj02Latitude());
		shelfInfo.setLontitude(bookShelf.getGcj02Longitude());
		
		StaffDistrictInfoDO districtInfo = staffDistrictInfoDAO.getByDistrictCode(bookShelf.getDistrictCode());
		String districtName = districtInfo.getDistrictName();
		shelfInfo.setDistrict_name(districtName);
		return shelfInfo;
	}

	@Override
	public LockSuccDTO lockSuccess(BookgridUnlockDTO unlock) throws BizException {
		
		Long userid = unlock.getUserid();
		Integer userType = unlock.getUserType();
		String bookgridCode = unlock.getGridCode();
		BookGridTransDO query = new BookGridTransDO();
		query.setUserId(userid);
		query.setUserType(userType);
		BookGridTransDO result = bookGridTransDAO.getByCondition(query);
		if(null == result)
			throw new BizException(RspCodeEnum.STAFF_TRANS_IS_NOT_EXISTS);
		Long transId = result.getId();
		StaffTransposingDO sposingDO = staffTransposingDAO.getByTransId(transId);
		if(null == sposingDO)
			throw new BizException(RspCodeEnum.STAFF_TRANS_IS_NOT_EXISTS);
		
		LockSuccDTO lockSucc = new LockSuccDTO();
		lockSucc.setBookgrid_code(bookgridCode);
		lockSucc.setPutoff_count(sposingDO.getPutoffCount());
		lockSucc.setPuton_count(sposingDO.getPutonCount());
		return lockSucc;
	}

	@Override
	public List<ChangeBookInfo> getTransposingList(ShelfListPage page) throws BizException {
		
		StaffTransposingPageDO pageDO = new StaffTransposingPageDO();
		pageDO.setStaffId(page.getUserid());
		pageDO.setOffset(page.getOffset());
		pageDO.setPagesize(page.getPagesize());
		List<StaffTransposingDO> list = staffTransposingDAO.listByUser(pageDO);
		if(ObjectUtils.isEmpty(list))
			throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
		
		List<ChangeBookInfo> infoList = new ArrayList<ChangeBookInfo>(list.size());
		for(StaffTransposingDO transposingDO : list) {
			ChangeBookInfo info = new ChangeBookInfo();
			info.setArea_name(staffShelfRegisterDAO.getByShelfCode(transposingDO.getBookgridCode().substring(0, 10)).getAreaName());
			info.setBookgrid_code(transposingDO.getBookgridCode());
			info.setBookshelf_code(transposingDO.getBookgridCode().substring(0, 10));
			info.setChange_time(DateFormatUtil.formatDateTime(transposingDO.getCreateTime()));
			info.setPutoff_count(transposingDO.getPutonCount());
			info.setPutoff_count(transposingDO.getPutoffCount());
			info.setSerialnumber(transposingDO.getBookgridCode().substring(10));
			infoList.add(info);
		}
		
		return infoList;
	}

	@Override
	public int getShelfListCount(ShelfListPage page) throws BizException {
		Long userid = page.getUserid();
		int count = staffShelfRegisterDAO.countByUserId(userid);
		return count;
	}

	@Override
	public int getTransposingListCount(ShelfListPage page) throws BizException {
		Long userid = page.getUserid();
		int count = staffTransposingDAO.histCountByUser(userid);
		return count;
	}

}
